package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;

public class GUIDBConfig extends JDialog{
	
	private static final long serialVersionUID = 1L;
	private GUIHome home;
	private JTextField tfServer;
	private JTextField tfDBName;
	private JTextField tfUser;
	private JPasswordField tfPass;
	private JButton btOK;
	private JButton btCancel;
	
	public GUIDBConfig(JFrame parent, GUIHome home){
		super(parent, "Cấu hình CSDL", true);
		this.home = home;
		tfServer = new JTextField();
		tfDBName = new JTextField();
		tfUser = new JTextField();
		tfPass = new JPasswordField();
		
		btOK = new CustomJButton("Kết nối", 120, 30, Color.WHITE, CustomJButton.DARKER, Color.DARK_GRAY, 2, false, true);
		btOK.addActionListener(new OKAction());
		btCancel = new CustomJButton("Thoát", 120, 30, Color.WHITE, CustomJButton.DARKER, Color.DARK_GRAY, 2, false, true);
		btCancel.addActionListener(new CancelAction());
		
		setLayout(new BorderLayout());
		JPanel panel = new JPanel();
		panel.setOpaque(false);
		panel.setBorder(new EmptyBorder(10, 10, 10, 10));
		panel.setLayout(new GridBagLayout());
		
		GridBagConstraints c = new GridBagConstraints();
		c.fill =  GridBagConstraints.BOTH;
		c.gridx = 0;
		c.gridy = 0;
		c.weightx = 0.2f;
		panel.add(new JLabel("Server : "),c);
		c.gridx = 1;
		c.weightx = 0.8f;
		panel.add(tfServer,c);
		
		c.gridx = 0;
		c.gridy = 1;
		c.weightx = 0.2f;
		panel.add(new JLabel("Tên DB : "),c);
		c.gridx = 1;
		c.weightx = 0.8f;
		panel.add(tfDBName,c);
		
		c.gridx = 0;
		c.gridy = 2;
		c.weightx = 0.2f;
		panel.add(new JLabel("Tên đăng nhập : "),c);
		c.gridx = 1;
		c.weightx = 0.8f;
		panel.add(tfUser,c);
		
		c.gridx = 0;
		c.gridy = 3;
		c.weightx = 0.2f;
		panel.add(new JLabel("Mật khẩu : "),c);
		c.gridx = 1;
		c.weightx = 0.8f;
		panel.add(tfPass,c);
		
		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
		buttonPanel.setOpaque(false);
		buttonPanel.add(btOK);
		buttonPanel.add(btCancel);
		
		add(panel,BorderLayout.CENTER);
		add(buttonPanel,BorderLayout.SOUTH);
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
	}
	
	class OKAction extends AbstractAction{
		
		private static final long serialVersionUID = 1L;
		private DataOutputStream os;

		@Override
		public void actionPerformed(ActionEvent arg0) {
			if(tfServer.getText().trim().equals("")){
				JOptionPane.showMessageDialog(GUIDBConfig.this, "Tên server không được để trống.",
						"Thông báo", JOptionPane.ERROR_MESSAGE);
				return;
			}
			if(tfDBName.getText().trim().equals("")){
				JOptionPane.showMessageDialog(GUIDBConfig.this, "Tên database không được để trống.",
						"Thông báo", JOptionPane.ERROR_MESSAGE);
				return;
			}
			if(tfUser.getText().trim().equals("")){
				JOptionPane.showMessageDialog(GUIDBConfig.this, "Tên đăng nhập không được để trống.",
						"Thông báo", JOptionPane.ERROR_MESSAGE);
				return;
			}
			if(tfPass.getPassword().toString().trim().equals("")){
				JOptionPane.showMessageDialog(GUIDBConfig.this, "Mật khẩu không được để trống.",
						"Thông báo", JOptionPane.ERROR_MESSAGE);
				return;
			}
			try {
				os = new DataOutputStream(new FileOutputStream("dbConfig.dbz"));
				os.writeUTF(tfServer.getText().trim());
				os.writeUTF(tfDBName.getText().trim());
				os.writeUTF(tfUser.getText().trim());
				os.writeUTF(String.valueOf(tfPass.getPassword()));
				os.flush();
				os.close();
				dispose();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	class CancelAction extends AbstractAction{
		
		private static final long serialVersionUID = 1L;

		@Override
		public void actionPerformed(ActionEvent e) {
			home.setCancel(true);
			dispose();
		}
	}
}
